package com.fc.mall.portal.service.impl;

import com.fc.mall.mapper.UmsMemberBankMapper;
import com.fc.mall.model.UmsMember;
import com.fc.mall.model.UmsMemberBank;
import com.fc.mall.model.UmsMemberBankExample;
import com.fc.mall.portal.service.UmsMemberBankService;
import com.fc.mall.portal.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Sunny on 2019/7/4.
 */

@Service
public class UmsMemberBankServiceImpl implements UmsMemberBankService {

    @Autowired
    private UmsMemberService memberService;

    @Autowired
    private UmsMemberBankMapper memberBankMapper;

    @Override
    public int saveOrUpdateCardInfo(UmsMemberBank memberBank) {
        UmsMember currentMember = memberService.getCurrentMember();
        UmsMemberBankExample example = new UmsMemberBankExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId());

        List<UmsMemberBank> banks = memberBankMapper.selectByExample(example);

        if(null == banks || banks.isEmpty())
        {
            memberBank.setMemberId(currentMember.getId());
            return memberBankMapper.insertSelective(memberBank);
        }else {
            return memberBankMapper.updateByExampleSelective(memberBank, example);
        }
    }

    @Override
    public UmsMemberBank getMemberBank() {
        UmsMember currentMember = memberService.getCurrentMember();
        UmsMemberBankExample example = new UmsMemberBankExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId());

        List<UmsMemberBank> banks = memberBankMapper.selectByExample(example);
        if(null == banks || banks.isEmpty())
        {
            return new UmsMemberBank();
        }
        return banks.get(0);
    }
}
